package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.entity.SysRole;
import com.blog.entity.SysUser;
import com.blog.service.SysRoleService;
import com.blog.util.StringUtil;

import com.blog.util.dto.AjaxResult;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 系统角色控制类
 * @author cgx
 * @Date 2022/9/1 21:53
 */
@Controller
@Slf4j
@RequestMapping("/role")
public class SysRoleController {


    @Resource
    SysRoleService roleService;

    /**
     * 后台获取表格数据
     * @param
     * @return
     */
    @GetMapping("/admin/tableList")
    @ResponseBody
    public LayDto tableList(){
        List<SysRole> list = roleService.list();
        return LayDtoUtil.returnSuccess(0,"",list.size(),list);
    }

    /**
     * 后台表格重载
     * @param
     * @return
     */
    @GetMapping("/admin/queryRoleByCondition")
    @ResponseBody
    public LayDto tableReloadList(@RequestParam("status") Integer status,@RequestParam("roleName") String roleName,@RequestParam("roleSign") String roleSign) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if (status != 0) {
            queryWrapper.eq("status", status);
        }
        if (StringUtil.isNotBlank(roleName)) {
            queryWrapper.like("role_name", roleName);
        }
        if (StringUtil.isNotBlank(roleSign)) {
            queryWrapper.like("role_sign", roleSign);
        }
        List<SysRole> list = roleService.list(queryWrapper);
        return LayDtoUtil.returnSuccess(0, "", list.size(), list);
    }


    /**
     * 编辑页面跳转
     * @param
     * @return
     */
    @GetMapping("/admin/role-edit")
    public String editRole(@RequestParam("roleId") Long roleId, Model model){
        model.addAttribute("roleId",roleId);
        SysRole role = roleService.getById(roleId);
        model.addAttribute("role",role);
        return "admin/system/role/role-edit";
    }

    /**
     * 获取角色信息通过id
     * @param roleId
     * @return
     */
    @GetMapping("/admin/queryRoleInfo")
    @ResponseBody
    public AjaxResult queryRoleById(@RequestParam("roleId") Long roleId){
        SysRole byId = roleService.getRoleInfo(roleId);
        return AjaxResult.success(byId);
    }

    @PostMapping("/admin/updateById")
    @ResponseBody
    public AjaxResult updateById(@RequestBody SysRole role){
        roleService.updateById(role);
        return AjaxResult.success();
    }

    @PostMapping("/admin/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(@RequestBody SysRole role){
        SysRole sysRole = roleService.getById(role.getRoleId());
        if ("admin".equals(sysRole.getRoleSign())){
            return AjaxResult.error("不允许操作管理员");
        }
        roleService.updateById(role);
        return AjaxResult.success();
    }


    @GetMapping("/admin/getMenuByRole")
    @ResponseBody
    public AjaxResult getMenuByRole(){
        return roleService.getMenuByRole();
    }


    @PostMapping("/admin/addRole")
    @ResponseBody
    public AjaxResult addRole(@RequestBody SysRole role){
        Long[] menuIds = role.getMenuIds();
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser)subject.getPrincipal();
        role.setCreateBy(sysUser.getUsername());
        role.setActive(1);
        roleService.save(role);
        Long roleId = role.getRoleId();
        roleService.saveRoleMenu(roleId,menuIds);
        return AjaxResult.success();
    }


    @PostMapping("/admin/deleteRole")
    @ResponseBody
    public AjaxResult deleteRole(@RequestBody List<Long> roleIds){
        roleService.deleteRole(roleIds);
        return AjaxResult.success(roleIds);
    }

    @PostMapping("/admin/updateRole")
    @ResponseBody
    public AjaxResult updateRole(@RequestBody SysRole role){
        roleService.updateRole(role);
        return AjaxResult.success(role);
    }
}
